package kr.co.casually.adapter;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ConcertDbAdapter {
	public static final String KEY_ROWID = "_id"; // idx
	public static final String KEY_CULT_CODE = "cultcode"; // 공연코드
	public static final String KEY_GENRE_CODE = "genrecode"; // 장르코드
	public static final String KEY_GENRE_NAME = "genrename"; // 장르명
	public static final String KEY_TITLE = "title"; // 명칭
	public static final String KEY_STARTDATE = "startdate"; // 시작일
	public static final String KEY_ENDDATE = "enddate"; // 종료일
	public static final String KEY_PLACE = "place"; // 공연장소
	public static final String KEY_IMAGE = "image"; // 이미지

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "seoulconcert_concert.db";
	private static final String DATABASE_TABLE = "concert";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (" + KEY_ROWID
			+ " integer primary key autoincrement, " + KEY_CULT_CODE
			+ " INTEGER, " + KEY_GENRE_CODE + " INTEGER, " + KEY_GENRE_NAME
			+ " TEXT, " + KEY_TITLE + " TEXT, " + KEY_STARTDATE + " DATE, "
			+ KEY_ENDDATE + " DATE, " + KEY_PLACE + " TEXT, " + KEY_IMAGE
			+ " TEXT);";

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			db.execSQL(DATABASE_CREATE);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			/*Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");*/
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
	}

	public ConcertDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public ConcertDbAdapter open() throws SQLException {

		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	// 공연정보 입력
	public long createConcert(int cultCode, int genreCode, String genreName,
			String title, String startDate, String endDate, String place,
			String imagePath) throws ClientProtocolException, IOException {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_CULT_CODE, cultCode);
		initialValues.put(KEY_GENRE_CODE, genreCode);
		initialValues.put(KEY_GENRE_NAME, genreName);
		initialValues.put(KEY_TITLE, title);
		initialValues.put(KEY_STARTDATE, startDate);
		initialValues.put(KEY_ENDDATE, endDate);
		initialValues.put(KEY_PLACE, place);
		initialValues.put(KEY_IMAGE, imagePath);

		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	// 공연정보 삭제
	public boolean deleteConcert(long rowId) {
//		Log.i("Delete called", "value__" + rowId);
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public void clearConcertTable() {
		mDb.delete(DATABASE_TABLE, null, null);
	}

	// 공연정보 모두 가져오기
	public Cursor fetchAllConcert() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_CULT_CODE, KEY_GENRE_CODE, KEY_GENRE_NAME, KEY_TITLE,
				KEY_STARTDATE, KEY_ENDDATE, KEY_PLACE, KEY_IMAGE }, null, null,
				null, null,  KEY_CULT_CODE + " desc");
	}

	// 공연정보 1줄 받아오기
	public Cursor fetchConcert(long rowId) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_CULT_CODE, KEY_GENRE_CODE, KEY_GENRE_NAME, KEY_TITLE,
				KEY_STARTDATE, KEY_ENDDATE, KEY_PLACE, KEY_IMAGE }, KEY_ROWID
				+ "=" + rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	// 공연정보 CultCode로 유무 확인하기
	public boolean isConcert(int cultCode) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_CULT_CODE, KEY_GENRE_CODE, KEY_GENRE_NAME, KEY_TITLE,
				KEY_STARTDATE, KEY_ENDDATE, KEY_PLACE, KEY_IMAGE },
				KEY_CULT_CODE + "=" + cultCode, null, null, null,null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return (mCursor.getCount() != 0) ? true : false;

	}

	// 공연정보 수정
	public boolean updateConcert(long rowId, int cultCode, int genreCode,
			String genreName, String title, String startDate, String endDate,
			String place, String imagPath) {
		ContentValues args = new ContentValues();
		args.put(KEY_CULT_CODE, cultCode);
		args.put(KEY_GENRE_CODE, genreCode);
		args.put(KEY_GENRE_NAME, genreName);
		args.put(KEY_TITLE, title);
		args.put(KEY_STARTDATE, startDate);
		args.put(KEY_ENDDATE, endDate);
		args.put(KEY_PLACE, place);
		args.put(KEY_IMAGE, imagPath);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}

	// 키워드 등록 조건 검색 쿼리
	public Cursor selectMyConcertList(String where) {
		String query = "SELECT " + KEY_ROWID + " , " + KEY_CULT_CODE + " , "
				+ KEY_GENRE_CODE + " , " + KEY_GENRE_NAME + " , " + KEY_TITLE
				+ " , " + KEY_STARTDATE + " , " + KEY_ENDDATE + " , "
				+ KEY_PLACE + " , " + KEY_IMAGE + " FROM " + DATABASE_TABLE
				+ " " + where + " order by " +  KEY_CULT_CODE + " desc";
		Cursor c = mDb.rawQuery(query, null);

		return c;
	}

}
